package com.shyroke.daydayzhuansvc.service.impl;

import com.shyroke.daydayzhuanapi.dto.SysDownDto;
import com.shyroke.daydayzhuanapi.entity.SysDownEntity;
import com.shyroke.daydayzhuanapi.mapper.SysDownMapper;
import com.shyroke.daydayzhuandao.respository.SysDownRepository;
import com.shyroke.daydayzhuansvc.service.ISysDownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 类名 ：
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2019/1/29 9:27
 */
@Service
public class SysDownService implements ISysDownService {

    @Autowired
    private SysDownRepository sysDownRepository;
    @Autowired
    private SysDownMapper sysDownMapper;

    @Override
    public void addCount() {
        Optional<SysDownEntity> optionalSysDownEntity = sysDownRepository.findById(1);
        if(optionalSysDownEntity.isPresent()){
            SysDownEntity sysDownEntity = optionalSysDownEntity.get();
            int count = sysDownEntity.getCount();
            int todayCount = sysDownEntity.getTodayCount();
            sysDownEntity.setCount(count+1);
            sysDownEntity.setTodayCount(todayCount+1);
            sysDownRepository.save(sysDownEntity);
        }
    }

    @Override
    public void clearCount() {
        Optional<SysDownEntity> optionalSysDownEntity = sysDownRepository.findById(1);
        if(optionalSysDownEntity.isPresent()){
            SysDownEntity sysDownEntity = optionalSysDownEntity.get();
            sysDownEntity.setTodayCount(0);
            sysDownRepository.save(sysDownEntity);
        }
    }

    @Override
    public SysDownDto getSysDown() {
        Optional<SysDownEntity> optionalSysDownEntity = sysDownRepository.findById(1);
        if(optionalSysDownEntity.isPresent()){
            SysDownEntity sysDownEntity = optionalSysDownEntity.get();
            return sysDownMapper.entityToDto(sysDownEntity);
        }else {
            return null;
        }
    }
}
